package basic.com.liz.controller.business;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import basic.com.liz.bean.entity.business.Picture;
import basic.com.liz.common.utils.HttpResult;
import basic.com.liz.common.utils.Servlets;
import basic.com.liz.service.business.PictureService;

/**
 * <图片处理>
 * 
 * @author 陆小凤
 * @version [1.0, 2015年9月11日]
 */
@Slf4j
@Controller
@RequestMapping(value = "/picture")
public class PictureController extends BusinessBasicController
{
    
    @Autowired
    private PictureService pictureService;
    
    /**
     * <查询图片信息>
     * 
     * @param pageNumber
     * @param pageSize
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "list")
    public String pictureList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
        @RequestParam(value = "numPerPage", defaultValue = "10") int numPerPage, Model model, ServletRequest request)
    {
        // 获取查询条件
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        try
        {
            // 根据条件查询角色信息
            Page<Picture> pictureList = pictureService.getPictureList(searchParams, pageNum, numPerPage);
            model.addAttribute("pictureList", pictureList.getContent());
            model.addAttribute("totalCount", pictureList.getTotalElements());
            model.addAttribute("pageNum", pageNum);
            model.addAttribute("numPerPage", numPerPage);
        }
        catch (Exception e)
        {
            log.error("Enter func[pictureList] catch exception while get systemConfigList ", e);
        }
        return "business/picture/pictureList";
    }
    
    /**
     * <进入系统配置添加页面>
     * 
     * @return
     */
    @RequestMapping(value = "add")
    public String addConfig(ModelMap model)
    {
        return "business/picture/pictureInfo";
    }
    
    /**
     * <进入编辑页面>
     * 
     * @param topicId
     * @param model
     * @return
     */
    @RequestMapping(value = "edit")
    public String editInfo(@RequestParam(value = "id") Long id, ModelMap model)
    {
        Picture picture = new Picture();
        try
        {
            picture = pictureService.getPictureById(id);
            model.addAttribute("picture", picture);
        }
        catch (Exception e)
        {
            log.error("Enter func[editInfo] catch exception ", e);
        }
        return "business/picture/pictureInfo";
    }
    
    /**
     * <保存 图片信息>
     * 
     * @param menu
     * @param model
     * @param response
     * @return
     */
    @RequestMapping(value = "savePicture")
    @ResponseBody
    public HttpResult savePicture(@Valid @ModelAttribute("picture") Picture picture, ModelMap model,
        HttpServletRequest request, HttpServletResponse response)
    {
        HttpResult result = new HttpResult();
        try
        {
            pictureService.save(picture);
            // 返回正确的的结果
            result = getCorrectResult();
        }
        catch (Exception e)
        {
            // 返回错误的结果
            result = getErrorResult();
            log.error("Enter func[savePicture] catch exception", e);
        }
        return result;
        
    }
    
    /**
     * <删除图片>
     * 
     * @param response
     * @param request
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public HttpResult deleteById(@RequestParam(value = "id") Long id)
    {
        HttpResult result = new HttpResult();
        
        try
        {
            pictureService.deleteById(id);
            // 返回正确的的结果
            result = getCorrectResult();
        }
        catch (Exception e)
        {
            // 返回错误的结果
            result = getErrorResult();
            log.info("Enter func[deleteById] catch exception", e);
        }
        return result;
    }
    
}
